#include "IMain.h"
#include "ui_imain.h"
#include "ui_character.h"
#include "ui_connexion.h"
#include "ui_create_character.h"
#include "ui_create_monster.h"
#include "ui_monster.h"
#include "ui_monster_group.h"
#include "ui_summary.h"

IMain::IMain(QWidget *parent) :
    QMainWindow(parent),
    mainFrame(new Ui::IMain)
{
    isAdmin = false;
    mainFrame->setupUi(this);
    connexionFrame      = new Ui::Connexion;
    connexion           = new QWidget;
    chat                = new Chat();
    th_links            = new ThreadLinks();
    characFrame         = new Ui::Character;
    DB                  = new DataBase();
    admin               = new Administrator();
    mainConnexion();

}

void IMain::adminSpells()
{
    admin->displaySpellsAdmin();
    admin->show();
}

void IMain::adminMonsters()
{

}
void IMain::adminMembers()
{

}

void IMain::clientChangeCharac()
{

}

void IMain::clientCharacSheet()
{

}

void IMain::clientCharacStory()
{

}

void IMain::mainConnexion()
{
    connexionFrame->setupUi(connexion);
    connect(connexionFrame->buttonClient,   SIGNAL(clicked()), this, SLOT(mainClient()));
    connect(connexionFrame->buttonAdmin, SIGNAL(clicked()), this, SLOT(mainServer()));
    connexionFrame->lineEdit_7->setText("32767");
    connexionFrame->lineEdit_5->setText("32767");
    connexion->show();
}

void IMain::startChat()
{
    connect(chat,       SIGNAL(newMsg(QString)),       th_links, SLOT(messageAll(QString)));
    connect(th_links,   SIGNAL(new_msg_chat(QString)), chat,     SLOT(displayMsg(QString)));
    chat->show();
}

void IMain::startCharacter()
{

    QWidget *character               = new QWidget();
    characFrame->setupUi(character);
    character->setStyleSheet("background-image: url(./Img/perso1.png); "
                             "background-position: top left; "
                             "background-repeat: no-repeat;");
    character->show();
}

IMain::~IMain()
{
    delete mainFrame;
    delete chat;
    delete th_links;
    delete connexionFrame;
    delete connexion;
}

void IMain::mainClient()
{
    connect(mainFrame->actionChanger_de_personnage, SIGNAL(triggered()), this, SLOT(clientChangeCharac()));
    connect(mainFrame->actionFiche_personnage, SIGNAL(triggered()), this, SLOT(clientCharacSheet()));
    connect(mainFrame->actionHistoire, SIGNAL(triggered()), this, SLOT(clientCharacStory()));

    th_links->setIpServer(connexionFrame->lineEdit_4->text());
    th_links->setPort(connexionFrame->lineEdit_5->text());
    th_links->createClient();
    connexion->hide();
    this->show();
}

void IMain::mainServer()
{
    connect(mainFrame->actionPouvoirs, SIGNAL(triggered()), this, SLOT(adminSpells()));
    connect(mainFrame->actionMonstres, SIGNAL(triggered()), this, SLOT(adminMonsters()));
    connect(mainFrame->actionMembres, SIGNAL(triggered()), this, SLOT(adminMembers()));
    connect(mainFrame->actionChanger_de_personnage, SIGNAL(triggered()), this, SLOT(clientChangeCharac()));
    connect(mainFrame->actionFiche_personnage, SIGNAL(triggered()), this, SLOT(clientCharacSheet()));
    connect(mainFrame->actionHistoire, SIGNAL(triggered()), this, SLOT(clientCharacStory()));

    th_links->setPort(connexionFrame->lineEdit_7->text());
    th_links->createServer();
    connexion->hide();
    this->show();
    isAdmin = true;
}
